package io.rong.app.model;

import android.text.TextUtils;

import com.yunshipei.common.Globals;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.Serializable;

public class GroupCreateInfo implements Serializable, Cloneable {
    private String id = "";            //群id
    private String portraitUri;    //群头像
    private String name;            //群名称
    private String introduce;    //群简介
    private int number = 0;            //群当前人数
    private int max_number = Globals.GROUPMAX;        //群最大人数
    private String create_user_id;        //群创建人id
    private String create_dateTime;        //群创建时间
    private String groupMembers;        //群成员ids

    public String getGourpMembers() {
        return groupMembers;
    }

    public void setGourpMembers(String gourpMember) {
        if (!TextUtils.isEmpty(gourpMember)) {
            this.groupMembers = gourpMember;
            setNumber(computeNumber(gourpMember));
        } else {
            this.groupMembers = "[]";
            setNumber(0);
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIntroduce() {
        return introduce;
    }

    public void setIntroduce(String introduce) {
        this.introduce = introduce;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getMax_number() {
        return max_number;
    }

    public void setMax_number(int max_number) {
        this.max_number = max_number;
    }

    public String getCreate_user_id() {
        return create_user_id;
    }

    public void setCreate_user_id(String create_user_id) {
        this.create_user_id = create_user_id;
    }

    public String getCreate_dateTime() {
        return create_dateTime;
    }

    public void setCreate_dateTime(String create_dateTime) {
        this.create_dateTime = create_dateTime;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPortraitUri() {
        return portraitUri;
    }

    public void setPortraitUri(String portraitUri) {
        this.portraitUri = portraitUri;
    }

    public int computeNumber(String gourpMember) {
        try {
            JSONArray array = new JSONArray(getGourpMembers());
            return array.length();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public GroupCreateInfo clone() {
        GroupCreateInfo groupCreateInfo = null;
        try {
            groupCreateInfo = (GroupCreateInfo) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return groupCreateInfo;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o != null && o instanceof GroupCreateInfo) {
            GroupCreateInfo gci = (GroupCreateInfo) o;
            String id = gci.getId();
            String portraitUri = gci.getPortraitUri();
            String name = gci.getName();
            String introduce = gci.getIntroduce();
            String createUserId = gci.getCreate_user_id();
            String groupMembers = gci.getGourpMembers();
            String createDataTime = gci.getCreate_dateTime();
            if (!TextUtils.isEmpty(id) && !TextUtils.isEmpty(portraitUri) && !TextUtils.isEmpty(name) && !TextUtils.isEmpty(introduce) && !TextUtils.isEmpty(createUserId) && !TextUtils.isEmpty(groupMembers) && !TextUtils.isEmpty(createDataTime)) {
                if (this.id.equals(id) && this.portraitUri.equals(portraitUri)
                        && this.name.equals(gci.getName()) && this.introduce.equals(gci.getIntroduce())
                        && this.number == gci.getNumber() && this.max_number == gci.max_number
                        && this.create_user_id.equals(gci.getCreate_user_id()) && this.groupMembers.equals(gci.getGourpMembers())
                        && this.create_dateTime.equals(gci.getCreate_dateTime())) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        int result = id.hashCode();
        result = 31 * result + portraitUri.hashCode();
        result = 31 * result + name.hashCode();
        result = 31 * result + introduce.hashCode();
        result = 31 * result + number;
//        result = 31 * result + max_number;
        result = 31 * result + create_user_id.hashCode();
        result = 31 * result + create_dateTime.hashCode();
        result = 31 * result + groupMembers.hashCode();
        return result;
    }
}
